package joSaysHello.server;

import java.util.HashSet;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserManager {
	
	private static final UserService userService = UserServiceFactory.getUserService();
	
	public static joSaysHello.shared.entity.User getCurrentUser(PersistenceManager pm)
	{
		com.google.appengine.api.users.User gaeUser = userService.getCurrentUser();
		if(gaeUser == null)
			return null;
		joSaysHello.shared.entity.User joUser;
		try
		{
			joUser = pm.getObjectById(joSaysHello.shared.entity.User.class, gaeUser.getUserId());
		}
		catch(JDOObjectNotFoundException e)
		{
			joUser = new joSaysHello.shared.entity.User(gaeUser.getUserId(), gaeUser.getNickname());
			pm.makePersistent(joUser);
		}
		return joUser;
	}
	
	public static String getLoginURL(String openIdUrl, String backURL) {
		return userService.createLoginURL(backURL, null, openIdUrl, new HashSet<String>());
	}
	public static String getLogoutURL(String backURL) {
		return userService.createLogoutURL(backURL, null);
	}
}
